package com.haikesi.api.user.controller;

import com.github.pagehelper.PageInfo;
import com.haikesi.api.base.*;
import com.haikesi.api.base.Error;
import com.haikesi.api.base.annotation.Token;
import com.haikesi.api.menuauthority.domain.vo.MenuAuthoritySearchVO;
import com.haikesi.api.menuauthority.domain.vo.MenuAuthorityTreeVO;
import com.haikesi.api.menuauthority.domain.vo.MenuAuthorityVO;
import com.haikesi.api.role.domain.vo.RoleAuthorityVO;
import com.haikesi.api.user.domain.vo.*;
import com.haikesi.api.user.service.IUserService;
import com.haikesi.util.IpUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @version V1.0
 * @ClassName: UserController
 * @author: zqy
 * @date: 2019年4月28日 上午9:17:54
 * @Copyright: 2019 重庆海克斯科技有限公司.
 * 注意：本内容仅限于重庆海克斯科技有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
@Api(description = "用户信息相关接口", tags = ApiIndex.UserController)
@RequestMapping(value = "/api/user")
@RestController
public class UserController {

    @Autowired
    IUserService userService;

    @ApiOperation(value = "用户登录", notes = "登录后取到ticket凭证,调用\"创建token\"接口得到token\n(admin/670b14728ad9902aecba32e22fa4f6bd)")
    @PostMapping(value = "/login")
    public R<UserLoginResVO> login(
            HttpServletRequest request,
            @ApiParam(value = "查询参数") @Valid @RequestBody UserLoginReqVO userLoginReqVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        userLoginReqVO.setLoginIp(IpUtil.getIpAddr(request));
        UserLoginResVO userLoginResVO = userService.login(userLoginReqVO);
        return new R(userLoginResVO);
    }

    @ApiOperation(value = "创建token")
    @PostMapping(value = "/token")
    @ResponseBody
    public R<UserTokenResVO> token(
            @ApiParam(value = "参数") @Valid @RequestBody UserTicketReqVO userTicketReqVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        UserTokenResVO vo = userService.getToken(userTicketReqVO.getTicket());
        return new R(vo);
    }


    @ApiOperation(value = "刷新token")
    @PutMapping(value = "/token")
    public R<UserTokenResVO> token(
            @Valid @RequestBody UserTokenReqVO userTokenReqVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        UserTokenResVO vo = userService.refreshToken(userTokenReqVO.getToken());
        return new R(vo);
    }


    @ApiOperation(value = "清除token")
    @PutMapping(value = "/cleanToken")
    public R cleanToken(
            @Valid @RequestBody UserTokenReqVO userTokenReqVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        userService.cleanToken(userTokenReqVO);
        return new R();
    }



    @ApiOperation(value = "查询列表")
    @GetMapping(value = "/list")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R<PageInfo<List<UserVO>>> list(
            @ApiParam(value = "查询参数") @ModelAttribute UserSearchVO searchVO) {
        List<UserVO> list = userService.getList(searchVO);
        PageInfo pageInfo = new PageInfo(list);
        return new R(pageInfo);
    }


    @ApiOperation(value = "创建用户")
    @PostMapping(value = "")
    public R create(
            @ApiParam(value = "添加参数") @Valid @RequestBody UserSaveVO userSaveVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        return new R().setData(userService.create(userSaveVO));
    }


    @ApiOperation(value = "修改用户")
    @PutMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R update(
            @ApiParam(value = "修改参数") @Valid @RequestBody UserUpdateVO updateVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        userService.update(updateVO);
        return new R();
    }



    @ApiOperation(value = "修改用户密码")
    @PutMapping(value = "updatePassword")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R updatePassword(
            @ApiParam(value = "修改参数") @Valid @RequestBody UserUpdatePasswordVO updateVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        userService.updatePassword(updateVO);
        return new R();
    }




    @ApiOperation(value = "删除用户")
    @DeleteMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R delete(
            @ApiParam(value = "修改参数") @RequestParam String id) {
        userService.delete(id);
        return new R();
    }


    @ApiOperation(value = "菜单树形")
    @GetMapping(value = "/menuTree")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R<List<MenuAuthorityTreeVO>> getmenuTree(
            @ApiParam(value = "是否菜单 | 用来区分菜单和分配权限的区别") @RequestParam  String isMenu
    ) {
        List<MenuAuthorityTreeVO> list = userService.menuTree( isMenu);
        return new R(list);
    }
    @ApiOperation(value = "按钮菜单集合")
    @GetMapping(value = "/buttonMenuList")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R<List<MenuAuthorityVO>> getButtonMenuList() {
        List<MenuAuthorityVO> list = userService.getButtonMenuList();
        return new R(list);
    }

    @ApiOperation(value = "根据用户id获取组织机构id")
    @GetMapping(value = "/getOrg")
//    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
//    @Token
    public String getOrg(String id) {
        String org = userService.getOrg(id);
        return org;
    }

}
